'''
Created on Oct 10, 2012

@author: daeheehan
'''

from github import Github
from github import Issue
from github import Repository
from datetime import datetime

# Github User information
GITHUB_USER_ID = "booknara"
GITHUB_USER_PW = "qwer1234"

# Github Bootstrap Repository information
GITHUB_BOOTSTRAP_ORG_NAME = "twitter"
GITHUB_BOOTSTRAP_REPOSITORY_NAME = "bootstrap"

# Github Bootstrap Issue filtering in the repository
ISSUE_BOOTSTRAP_URL = "https://github.com/twitter/bootstrap/issues/"
ISSUE_BOOTSTRAP_START = 5235
ISSUE_BOOTSTRAP_END = 5478

# Github Homebrew Repository information
GITHUB_HOMEBREW_ORG_NAME = "mxcl"
GITHUB_HOMEBREW_REPOSITORY_NAME = "homebrew"

# Github Homebrew Issue filtering in the repository
ISSUE_HOMEBREW_URL = "https://github.com/mxcl/homebrew/issues/"
ISSUE_HOMEBREW_START = 1
ISSUE_HOMEBREW_END = 15389 

# Github Rails Repository information
GITHUB_RAILS_ORG_NAME = "rails"
GITHUB_RAILS_REPOSITORY_NAME = "rails"

# Github Rails Issue filtering in the repository
ISSUE_RAILS_URL = "https://github.com/rails/rails/issues/"
ISSUE_RAILS_START = 1
ISSUE_RAILS_END = 7898 
ISSUE_RAILS_TEMP = 4112

# Flags for issue filtering
ISSUE_MILESTONE = "*"
ISSUE_CLOSE_STATE = "close"
ISSUE_OPEN_STATE = "open"
ISSUE_ASSIGNEE = "*"

ISSUE_CLOSED_EVENT = 'closed'
ISSUE_REOPENED_EVENT = 'reopened'
ISSUE_MERGED_EVENT = 'merged'

BOOTSTRAP_FILE_NAME = 'result_bootstrap.txt'
HOMEBREW_FILE_NAME = 'result_homebrew.txt'
RAILS_FILE_NAME = 'result_rails.txt'
    
if __name__ == '__main__':
    pass

#g = Github(GITHUB_USER_ID, GITHUB_USER_PW)
g = Github()
#print g.rate_limiting

# This part is for collecting bootstrap reopened issue
org = g.get_organization(GITHUB_BOOTSTRAP_ORG_NAME)
print org.url

fin = open(BOOTSTRAP_FILE_NAME, "r")
repo = org.get_repo(GITHUB_BOOTSTRAP_REPOSITORY_NAME)


while 1:
    lines = fin.readlines(100000)
    if not lines:
        break
    for line in lines:
        if len(line) != 0: 
            issue_temp = line[:-1]
            last_slash = issue_temp.rindex('/')
            issue_number = int(issue_temp[last_slash+1:])
        
            """
            # This part is for collecting bootstrap reopened issue's comments
            # Collecting the number of comments/replies before initially closed
            try:
                issue = repo.get_issue(issue_number)
                #print issue.url
                #print issue.url
                
                closetime = datetime.min
                for issueevent in issue.get_events():
                    if issueevent.event == ISSUE_CLOSED_EVENT:
                        closetime = issueevent.created_at
                        continue

                numofcomment = 0
                for issuecomment in issue.get_comments():
                    if issuecomment.created_at < closetime:
                        numofcomment += 1
                        
                print numofcomment

            except Exception  as inst: 
                print inst
            # End of collecting the number of comments/replies before initially closed
            """
            """
            # Collecting the number of distinct people before initially closed
            try:
                issue = repo.get_issue(issue_number)
                #print issue.url
                
                closetime = datetime.min
                for issueevent in issue.get_events():
                    if issueevent.event == ISSUE_CLOSED_EVENT:
                        closetime = issueevent.created_at
                        continue
                    
                listofpeople = []
                for issuecomment in issue.get_comments():
                    if issuecomment.created_at < closetime:
                        if issuecomment.user.name not in listofpeople:
                            listofpeople.append(issuecomment.user.name)
                        
                print listofpeople
                       
            except Exception  as inst: 
                print inst
            # End of collecting the number of distinct people before initially closed
            """
            
            
            try:
                issue = repo.get_issue(issue_number)
                #print issue.url
                
                closetime = datetime.min
                listofclosepeople = []
                for issueevent in issue.get_events():
                    if issueevent.event == ISSUE_CLOSED_EVENT:
                        closetime = issueevent.created_at
                        listofclosepeople.append(issueevent.actor.name)
                        
                print listofclosepeople
                print "created by " + issue.user.name
                print "closed by " + issue.closed_by.name
                 
            except Exception  as inst: 
                print inst
            
## This part is for collecting homebrew reopened issue
#org = g.get_organization(GITHUB_HOMEBREW_ORG_NAME)
##print org.url
#
#repo = org.get_repo(GITHUB_HOMEBREW_REPOSITORY_NAME)
#
#for i in range(ISSUE_HOMEBREW_START, ISSUE_HOMEBREW_END+1):
#    try: 
#        issue = repo.get_issue(i)
#        #print issue.url
#        for issueevent in issue.get_events():
#            if issueevent.event == ISSUE_REOPENED_EVENT:
#                #print issueevent.event
#                print issue.url
#                #print issue.number
#
#    except Exception:
#        print 'No information for an issue'
        


# This part is for collecting rails reopened issue
#org = g.get_organization(GITHUB_RAILS_ORG_NAME)
##print org.url
#
#repo = org.get_repo(GITHUB_RAILS_REPOSITORY_NAME)
#
#for i in range(ISSUE_RAILS_START, ISSUE_RAILS_END+1):
#    try: 
#        issue = repo.get_issue(i)
#        #print issue.url
#        for issueevent in issue.get_events():
#            if issueevent.event == ISSUE_REOPENED_EVENT:
#                #print issueevent.event
#                print issue.url
#                #print issue.number
#
#    except Exception:
#        print 'No information for an issue'
        
print

print "The End"